import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_nft/api/API.dart';
import 'package:flutter_nft/provider/LoginProvider.dart';
import 'package:provider/provider.dart';

class OrderList {
  late dynamic id;
  late String orderNo;
  late dynamic orderPrice;
  late String createTime;
  late String productCode;
  late String productTitle;
  late int initPrice;
  late String orgName;
  late String productImage;
  late int payStart;

  static OrderList? fromMap(Map<String, dynamic> map) {
    if (map == null) return null;
    OrderList orderListBean = OrderList();
    orderListBean.id = map['id'];
    orderListBean.orderNo = map['orderNo'];
    orderListBean.orderPrice = map['orderPrice'];
    orderListBean.createTime = map['createTime'];
    orderListBean.productCode = map['productCode'];
    orderListBean.productTitle = map['productTitle'];
    orderListBean.initPrice = map['initPrice'];
    orderListBean.orgName = map['orgName'];
    orderListBean.productImage = map['productImage'];
    orderListBean.payStart = map['payStart'];
    return orderListBean;
  }

  Map toJson() => {
        "id": id,
        "orderNo": orderNo,
        "orderPrice": orderPrice,
        "createTime": createTime,
        "productCode": productCode,
        "productTitle": productTitle,
        "initPrice": initPrice,
        "orgName": orgName,
        "productImage": productImage,
        "payStart": payStart,
      };
}

class OrderIndex extends StatefulWidget {
  @override
  _OrderIndexState createState() => _OrderIndexState();
}

class _OrderIndexState extends State<OrderIndex> {
  late List<OrderList?> orderLists = [];

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getOrderList();
    print("打印传参....");
    print(context.read<LoginProvider>().getUserName());
  }

  getOrderList() async {
    var result = await new Dio().get(API.reqUrl +
        "/get/order/list/" +
        context.read<LoginProvider>().getUserName());
    print(result.toString());
    if (result.data["flag"]) {
      List<OrderList?> list = (result.data["dataObj"] as List<dynamic>)
          .map((e) => OrderList.fromMap((e as Map<String, dynamic>)))
          .toList();
      if (list.length > 0) {
        setState(() {
          orderLists = list;
        });
      }


    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        elevation: 0,
        title: Text("我的订单"),
        centerTitle: true,
      ),
      body: Padding(
          padding: EdgeInsets.all(15),
          child: Container(
            width: MediaQuery.of(context).size.width,
            height: MediaQuery.of(context).size.height,
            decoration: BoxDecoration(
              color: Colors.white24,
            ),
            child: ListView(
              children: orderLists.length > 0
                  ? orderLists
                      .map((e) => Padding(
                            padding: EdgeInsets.only(top: 10),
                            child: InkWell(onTap: (){
                              Navigator.of(context).pushNamed("payproductdetail",arguments: e!.productCode.toString());
                              context.read<LoginProvider>().setProductDetailCode(e.productCode);
                            },child: Container(
                              width: MediaQuery.of(context).size.width - 20,
                              height: 100,
                              decoration: BoxDecoration(
                                  borderRadius: BorderRadius.circular(16),
                                  color: Colors.black54),
                              //color: Colors.amber,
                              child: Padding(
                                padding: EdgeInsets.only(left: 10),
                                child: Row(
                                  children: [
                                    Container(
                                      height: 80,
                                      width: 80,
                                      decoration: BoxDecoration(
                                          color: Colors.green,
                                          image: DecorationImage(
                                              image: NetworkImage(
                                                  e!.productImage.toString()),
                                              fit: BoxFit.fill)),
                                    ),
                                    Padding(
                                      padding: EdgeInsets.only(
                                          left: 10, top: 8, bottom: 12),
                                      child: Container(
                                        child: Column(
                                          mainAxisAlignment:
                                          MainAxisAlignment.spaceAround,
                                          children: [
                                            Container(
                                              width: MediaQuery.of(context)
                                                  .size
                                                  .width -
                                                  140,
                                              height: 30,
                                              //color: Colors.orange,
                                              child: Text("" +
                                                  e.productTitle.toString()),
                                            ),
                                            //Text("....."),
                                            Container(
                                              //width: MediaQuery.of(context).size.width,
                                              width: MediaQuery.of(context)
                                                  .size
                                                  .width -
                                                  140,
                                              alignment: Alignment.centerRight,
                                              child: Text(
                                                e.payStart.toString() == "2"
                                                    ? "已支付"
                                                    : "未支付",
                                                style: TextStyle(color:e.payStart.toString() == "2"? Colors.green:Colors.redAccent),
                                              ),
                                            ),
                                            Container(
                                              width: MediaQuery.of(context)
                                                  .size
                                                  .width -
                                                  140,
                                              child: Row(
                                                mainAxisAlignment:
                                                MainAxisAlignment
                                                    .spaceBetween,
                                                children: [
                                                  Container(
                                                    alignment:
                                                    Alignment.bottomLeft,
                                                    width: 80,
                                                    height: 30,
                                                    child: Text("" +
                                                        e.orgName.toString()),
                                                    //color: Colors.blue,
                                                  ),
                                                  Container(
                                                    alignment:
                                                    Alignment.bottomRight,
                                                    width: 80,
                                                    height: 30,
                                                    child: Text("¥" +
                                                        e.orderPrice
                                                            .toString()),
                                                    //color: Colors.lightGreen,
                                                  )
                                                ],
                                              ),
                                            )
                                          ],
                                        ),
                                      ),
                                    )
                                  ],
                                ),
                              ),
                            ),),
                          ))
                      .toList()
                  : [
                      Container(
                          width: MediaQuery.of(context).size.width,
                          height: 300,
                          child: Center(
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: [
                                Text(
                                  "你的订单空空如也!,",
                                  style: TextStyle(
                                      fontSize: 20, color: Colors.white),
                                ),
                                InkWell(
                                  onTap: () {
                                    print("点我了...");
                                    Navigator.of(context).pushNamed("tabbar");
                                  },
                                  child: Text(
                                    "点我",
                                    style: TextStyle(
                                        fontSize: 20, color: Colors.orange),
                                  ),
                                ),
                                Text(
                                  "去抢购吧",
                                  style: TextStyle(
                                      fontSize: 20, color: Colors.white),
                                ),
                              ],
                            ),
                          )),
                    ],
            ),
          )),
    );
  }
}


